package com.elf.gwt.store.client.components;

import java.util.Iterator;

import com.elf.gwt.common.client.access.RequestParameter;
import com.elf.gwt.common.client.callback.SubComponentCallback;
import com.elf.gwt.common.client.components.CommonSimpleComponent;
import com.elf.gwt.common.client.model.AccountModel;
import com.elf.gwt.common.client.model.ContactUsMetaModel;
import com.elf.gwt.common.client.model.ContactUsModel;
import com.elf.gwt.common.client.subcomponent.CaptchaSubComponent;
import com.elf.gwt.common.client.subcomponent.CommonSubComponent;
import com.elf.gwt.common.client.subcomponent.ContactUsSubComponent;
import com.elf.gwt.common.client.utils.CommonUtils;
import com.elf.gwt.store.client.StoreComponentController;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Widget;

public class ContactUsComponent extends JSONServerComponent 
{
	protected String action = "";

	protected ContactUsMetaModel contactUsMetaModel = null;
	protected ContactUsModel contactUsModel = null;
	protected ContactUsSubComponent contactUsSubComponent = null;

	/**
	 * Constructor
	 */
	public ContactUsComponent(StoreComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix, null, false);
		setupTimer(30000);
		contactUsModel = new ContactUsModel();
		contactUsMetaModel = new ContactUsMetaModel();
		contactUsSubComponent = 
			new ContactUsSubComponent(
					controller, 
				contactUsMetaModel, 
				contactUsModel, 
				StoreComponentController.accountComponent.getAccountModel(),
				StoreComponentController.accountComponent.getAccountMetaModel(), 
				subComponentCallback);
	}

	public boolean updateFromHistoryParameters(String[] params)
	{
		//Don't look for new products
		CommonSimpleComponent alwaysAskComponent = controller.getComponent("NewProductsComponent", false);
		if (alwaysAskComponent != null) storeController.getMultipleResultsComponent().removeComponent(alwaysAskComponent);

		return super.updateFromHistoryParameters(params);
	}

	public void additionalRequestParameters()
	{
		addRequestParameter(new RequestParameter("action", action));

		if (action != null && (action.equals(ContactUsSubComponent.Submit)))
		{
			Iterator<String> iterElements = contactUsSubComponent.getElements().keySet().iterator();
			while (iterElements.hasNext())
			{
				String key = iterElements.next();
				Widget widget = contactUsSubComponent.getElements().get(key);
				String value = CommonUtils.getValue(widget);
				if (value != null && value.length() > 0) addRequestParameter(new RequestParameter(key, value));
				AccountModel accountModel = StoreComponentController.accountComponent.getAccountModel();
				if (accountModel == null || accountModel.getId() < 1) {
					CaptchaSubComponent captchaSubComponent = (CaptchaSubComponent) contactUsSubComponent.getElements().get("captcha");
					addRequestParameter(new RequestParameter("captcha_code", captchaSubComponent.getCaptchaCode()));
					addRequestParameter(new RequestParameter("captcha_session", captchaSubComponent.getCaptchaSession()));
				}
			} 
		}
		action = "";
	}

	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
		contactUsMetaModel.update(jsonObject);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		contactUsModel.update(jsonObject);
	}

	public void onBuilt()
	{
		setVisible(!controller.isInitializing());
	}

	public void buildFromModel() throws Exception
	{
		AccountModel accountModel = StoreComponentController.accountComponent.getAccountModel();
		if (accountModel == null || accountModel.getId() < 1) {
			Grid grid = new Grid(2,1);
			grid.setText(0, 0, storeController.getStoreConstants().ContactUsComponentNotLoggedIn());
			grid.setWidget(1, 0, contactUsSubComponent);
			panel.setWidget(1, 0, grid);
		}
		else {
			panel.setWidget(1, 0, contactUsSubComponent);
		}
		contactUsSubComponent.build();
		onBuilt();
	}

	protected SubComponentCallback subComponentCallback = 
		new SubComponentCallback() {

		public void onNavigate(CommonSubComponent subComponent) {
		}

		public void onSuccess(CommonSubComponent subComponent) {
			ContactUsSubComponent contactUsSubComponent = (ContactUsSubComponent)subComponent;
			action = contactUsSubComponent.getAction();
			build(true);
		}

		public void onError(CommonSubComponent subComponent, Throwable exception) {}
	};
}
